package qlsl.androiddesign.view.subview.commonview;

import java.util.ArrayList;
import java.util.List;

import qlsl.androiddesign.activity.commonactivity.MainActivity;
import qlsl.androiddesign.activity.commonactivity.SearchActivity;
import qlsl.androiddesign.adapter.baseadapter.FragmentPagerAdapter;
import qlsl.androiddesign.fragment.basefragment.BaseFragment;
import qlsl.androiddesign.fragment.commonfragment.FragmentTab1;
import qlsl.androiddesign.fragment.commonfragment.TabFragment;
import qlsl.androiddesign.fragment.subfragment.AttentionFragment;
import qlsl.androiddesign.fragment.subfragment.HotFragment;
import qlsl.androiddesign.fragment.subfragment.SquareFragment;
import qlsl.androiddesign.listener.GuidePageCenterChangeListener;
import qlsl.androiddesign.util.commonutil.DensityUtils;
import qlsl.androiddesign.view.baseview.FunctionView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qlsl.androiddesign.demo.R;

public class TabView1 extends FunctionView<MainActivity> {

	private FragmentTab1 fragment;

	private TextView[] tv_guides = new TextView[3];

	private ImageView iv_cursor;

	private ViewPager viewPager;

	private List<BaseFragment> list;

	private int preIndex;

	private GuidePageCenterChangeListener pageListener;

	public TabView1(FragmentTab1 fragment, ViewGroup rootView, ViewGroup contentView) {
		super((MainActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	public void onResume() {
		int index = viewPager.getCurrentItem();
		pageListener.doPageSelected(index);
		resumeTabView(index);
	}

	public void onTabResume() {
		int index = viewPager.getCurrentItem();
		pageListener.doPageSelected(index);
		resumeTabView(index);
	}

	public void initView(View view) {
		view.findViewById(R.id.title_view).setVisibility(View.GONE);
		view.findViewById(R.id.devider_view).setVisibility(View.GONE);
		tv_guides[0] = findViewById(R.id.tv_guide1);
		tv_guides[1] = findViewById(R.id.tv_guide2);
		tv_guides[2] = findViewById(R.id.tv_guide3);
		iv_cursor = findViewById(R.id.iv_cursor);
		viewPager = findViewById(R.id.viewPager);
		viewPager.setOffscreenPageLimit(3);
		setViewPagerAdapter(DensityUtils.dp2px(activity, 80));
	}

	public void initData() {

	}

	public void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.iv_left:
			showToast("正在开发中");
			break;
		case R.id.btn_search:
			startActivity(SearchActivity.class);
			break;
		case R.id.tv_guide1:
			doClickGuideView(view);
			break;
		case R.id.tv_guide2:
			doClickGuideView(view);
			break;
		case R.id.tv_guide3:
			doClickGuideView(view);
			break;
		default:
			int index = viewPager.getCurrentItem();
			list.get(index).onClick(view);
			break;
		}
	}

	private void setViewPagerAdapter(int offset) {
		list = new ArrayList<BaseFragment>();
		BaseFragment fragment1 = new HotFragment();
		BaseFragment fragment2 = new SquareFragment();
		BaseFragment fragment3 = new AttentionFragment();
		list.add(fragment1);
		list.add(fragment2);
		list.add(fragment3);
		FragmentManager manager = fragment.getChildFragmentManager();
		viewPager.setAdapter(new FragmentPagerAdapter(manager, list));
		viewPager.setCurrentItem(1);
		tv_guides[1].setActivated(true);
		pageListener = new GuidePageCenterChangeListener(tv_guides, iv_cursor, offset) {
			public void onPageSelected(int index) {
				super.onPageSelected(index);
				pauseTabView();
				resumeTabView(index);
				preIndex = index;
			};
		};
		viewPager.setOnPageChangeListener(pageListener);
	}

	private void doClickGuideView(View view) {
		for (int index = 0; index < tv_guides.length; index++) {
			if (view == tv_guides[index]) {
				viewPager.setCurrentItem(index);
				tv_guides[index].setActivated(true);
			} else {
				tv_guides[index].setActivated(false);
			}
		}
	}

	private void resumeTabView(int index) {
		TabFragment fragment = (TabFragment) list.get(index);
		if (fragment.getFunctionView() != null) {
			fragment.onTabResume();
		}
	}

	private void pauseTabView() {
		TabFragment fragment = (TabFragment) list.get(preIndex);
		if (fragment.getFunctionView() != null) {
			fragment.onTabPause();
		}
	}

}
